var currentDir = "";
/**
 * 站內选择
 * @returns
 */
function showFileBox(){
	$.ajax({
		type : 'POST',
		async : false,
		url : 'files/list?parent=' + currentDir,
		dataType : 'json',
		async:false,
		success : function(data) {
			$("#file-list-box table").empty();
			$.each(data,function(i, e){
				$("#file-list-box table").append("<tr><td class='left'><a class='a-btn' href='javascript:showChildren(\"" + e + "\");'>" + e + "</a></td></tr>");
			})
			$("#select-file-box").show().window('open');
		},
		error : function(data) {
			$.messager.alert('Info', '操作异常', 'info');
		}
	})
}

/**
 * 展示子目录
 * @param filename
 * @returns
 */
function showChildren(filename){
	if(filename.indexOf(".") > 0){
		//如果是文件，点击則直接选择
		$("#litpic").textbox("setValue", "/uploads" + currentDir + "/" + filename);
		$("#imgBox").attr("src", ctx + "uploads" + currentDir + "/" + filename);
		$("#imgBox").show();
		$("#select-file-box").show().window('close');
	}else{
		currentDir += "/" + filename;
		showFileBox();
	}
}

/**
 * 展示上级
 * @returns
 */
function showParent(){
	if(currentDir != ''){
		currentDir = currentDir.substr(0, currentDir.lastIndexOf("/"));
		showFileBox();
	}
}

/**
 * 显示上传窗口
 * @returns
 */
function showUploadBox(){
	$("#upload-file-box").show().window('open');
}

/**
 * 提交上传
 * @returns
 */
function submitUpload(){
	$('#fileUploadForm').form('submit',{
		onSubmit:function(){
			if($("#file").val() == ''){
				$.messager.alert('Warn', '请选择文件', 'warn');
				return false;
			}
		},
		success:function(data){
			data = JSON.parse(data);
			if(data.success){
				$("#litpic").textbox("setValue", "/uploads" + data.msg);
				$("#imgBox").attr("src", ctx + "uploads" + data.msg);
				$("#imgBox").show();
				$('#upload-file-box').dialog('close');
			}else{
				var data = eval('(' + data + ')');  
				$.messager.alert('Info', data.message, 'info');
			}
		}
	});
}
